function getURLParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
window.dok = 1; //zmianna mowiaca o tym ile plikow domyslnie na poczatku mozna dodac !! musi byc taka sama co parametr funcji pokazUpload $inputCount
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function getXmlHttp(){
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            return xmlHttp;
        } 
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                return xmlHttp;
            } 
            catch (e) {
                xmlHttp = null;
            }
        }
    }
    return xmlHttp;
}

function ajaxChangeClientStatusStateChanged(){
    var response = xmlHttp.responseText;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    
        if (response != "no" && response != "") {
            document.getElementById('clientContactsId').style.backgroundColor = '#' + response;
        } else {
            alert(__("Wystąpił błąd podczas zmiany. Sprobuj ponownie później."));
        }
    }
    
}

function ajaxChangeContactStatus(newStatid, contactId){
    xmlHttp = getXmlHttp();
    xmlHttp.onreadystatechange = ajaxChangeClientStatusStateChanged;
    xmlHttp.open("GET", "ajaxChangeContactStatus.php?stat_id=" + newStatid + "&cnt_id=" + contactId + "&sid=" + Math.random(), true);
    xmlHttp.send(null);
}

function goToURL(url){
    window.location = url;
}

function confirmAddMyClient(url){
    var answer = confirm(__('Firma zostanie usunięta z Twojej bazy a dodany do Bazy Głównej! Czy kontynuować?'));
    if (answer) {
        goToURL(url);
    }
}

function confirmAddMyClient2(){
    var answer = confirm(__('Firma zostanie usunięta z Twojej bazy a dodany do Bazy Głównej! Czy kontynuować?'));
    if (answer) {
        return CheckClientSubmit();
    }
    else {
        return false;
    }
}

function maxFieldLength(obj,len)
{
    if (obj.value.length>len)
    {
         alert(__('Za duży text. Max.')+len+ __('znaków'));
        obj.value = obj.value.substr(0, len);
    }
}

function showClientContactsOnly(tableId, str){
    table = document.getElementById(tableId);
    trs = table.getElementsByTagName('tr');
    // var myRegExp = //;
    for (i = 0; i < trs.length; i++) {
        tr_id = trs[i].getAttribute('id');
        if (tr_id != null && tr_id.search(str) == -1) {
            trs[i].style.display = 'none';
        }
        if (tr_id != null && tr_id.search(str) != -1) {
            trs[i].style.display = '';
        }
    }
}

function displayElementById(elemId){
    document.getElementById(elemId).style.display = 'block';
}

function CheckSearchSubmit(){
    if (document.forms[0].name.value == '') { alert(__('Musisz podać nazwę!')); return false;}
}

function chxShowHideDiv(chx, divId){
    //alert('jest'+chxValue);
    var div = document.getElementById(divId);
    if (!chx.checked) {
        div.style.display = 'block';
    }
    else 
        if (chx.checked) {
            div.style.display = 'none';
        }
}

function showHideElement(elemId){
    var div = document.getElementById(elemId);
    if (div.style.display != 'block') {
        div.style.display = 'block';
    }
    else {
        div.style.display = 'none';
    }
}

function isValidMailAdress(adress){
    var validMatch = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    return adress.match(validMatch);
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 *
 * Poprawki Lukasz Marcinczak
 */
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 9;

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
            return false;
    }
    // All characters are numbers.
    return true;
}

function trim(s){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") 
            returnString += c;
    }
    return returnString;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) 
            returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
    var bracket = 3
    strPhone = trim(strPhone)
    if (strPhone.indexOf("+") > 1) 
        return false
    if (strPhone.indexOf("-") != -1) 
        bracket = bracket + 1
    if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) 
        return false
    var brchr = strPhone.indexOf("(")
    if (strPhone.indexOf("(") != -1 && strPhone.charAt(brchr + 2) != ")") 
        return false
    if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) 
        return false
    s = stripCharsInBag(strPhone, validWorldPhoneChars);
    
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isValidPhoneNumber(phoneNumber){

    if ((phoneNumber == null) || (phoneNumber == "")) {
        return false
    }
    if (checkInternationalPhone(phoneNumber) == false) {
        return false
    }
    return true
}

/**
 * Funkcja zatwierdzająca formularz po uprzednim zapytaniu użytkownika
 *
 * @param String formId id formularza do zatwierdzenia
 * @param String confirmText komunikat z prośbą o potwierdzenie
 */
function askAndConfirmForm(formId, confirmText){
    var form = document.getElementById(formId);
    if (window.confirm(confirmText)) {
        form.submit();
    }
    else {
        return false;
    }
    
    
}

function filedMaxChars(obj, strLen){
    if (obj.value.length > strLen) {
        alert('Za duży text. Max. ' + strLen + ' znaków');
        obj.value = obj.value.substr(0, strLen);
    }
}

function showOtherInput(select, inputId){
    input = document.getElementById(inputId);
    if (select.value == "Inne") 
        input.style.display = 'block';
    else 
        input.style.display = 'none';
}

/**
 *  Checks if the given value exists in the array
 */
function in_array(needle, haystack, argStrict){


    var key = '', strict = !!argStrict;
    
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    }
    else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    
    return false;
}

function initilaizeIntervalFunctions(){
    htmlobj = $.ajax({
        url: "classes/ajax_actions.php?do=interval_functions",
        async: false
    })
    
    var module_names = htmlobj.responseText.split(",");
    var i = 0;
    for (i = 0; i < module_names.length; i++) {
    
        var module = module_names[i];
		if(module!='')
        	window[module + '_interval']();
    }
}